//index.js
//获取应用实例
const app = getApp();

var url = getApp().globalData.url;
Page({
  data: {
    start: 0,
    list: [],
    load: true
  },
  //点击跳转详情页
  tap:(e) =>{
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id
    })
  },
  //请求更多数据
  loadData: function(){
    this.setData({load: true});
    wx.request({
      url: url+ '/v2/movie/in_theaters?start='+ this.data.start +'&conut=20',
      header: { "content-type": "json"},
      success: (res) => {
        if(res.data.subjects.length !=0){
          res.data.subjects.forEach( item => {
                if(item.rating.average ==0){
                  item.rating.average = '暂无评分';
                }
           });
          this.setData({
            list: this.data.list.concat(res.data.subjects),
            start: this.data.start+20,
            load: false
          });
        }else {
          this.setData({load: false });
           wx.showToast({
            title: '已到底部',
            duration: 2000
          })
        }
      }
    })
  },
  //事件处理函数
  onLoad: function () {
    this.loadData();
  },
  //用户上拉触底事件
  onReachBottom: function(){
    this.loadData();
  }
 
})
